package es.kivitel.android.sms.gui;

import es.kivitel.android.sms.*;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Calendar;

import es.kivitel.android.sms.kernel.BriefEvent;
import es.kivitel.android.sms.kernel.DatabaseManager;
import es.kivitel.android.sms.kernel.Event;

public class EventAdapterActivity extends ListActivity {

    public static final String listingDate = "LISTING_DATE";
	private static final int MENU_BACK = 0;
	private static final int MENU_HELP = 1;
	private static final int MENU_QUIT = 2;
	private static final String TAG = EventAdapterActivity.class.getName();

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
       super.onCreate(icicle);       
       setContentView(R.layout.list_activity);
	   ArrayList<BriefEvent> evtList = new ArrayList<BriefEvent>();
	   Calendar date = Calendar.getInstance();
	   Bundle extras = getIntent().getExtras();
	   if(extras != null){
		   date.setTimeInMillis(extras.getLong(EventAdapterActivity.listingDate));
	   }
	   else
		   finish();
	   try {
		   DatabaseManager.initialize(this);
		   ArrayList<Event> evts= DatabaseManager.getEvents(date);
		   Log.i(TAG,"-------------EXISTING EVENTS = "+evts.size());
		   for(Event event:evts){
			   BriefEvent be = new BriefEvent(event.getComment(), event.getDestination(), event.getType(), (event.getSent()?1:0), event.getSendingDate());
			   evtList.add(be);
		   }
	} catch (Exception e) {
		Toast.makeText(this, "UNTRACEABLE ERROR: Unable to load events", Toast.LENGTH_LONG).show();
		System.out.print(e.getMessage().toUpperCase());
		e.printStackTrace();
	}
       EventAdapter listAdapter = new EventAdapter(this,evtList ); 
       setListAdapter( listAdapter );
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, EventAdapterActivity.MENU_HELP, 0, getResources().getString(R.string.MenuHelp)).setIcon(android.R.drawable.ic_menu_info_details);        
        menu.add(0, EventAdapterActivity.MENU_BACK, 0, getResources().getString(R.string.MenuBack)).setIcon(android.R.drawable.ic_menu_revert);
        menu.add(0, EventAdapterActivity.MENU_QUIT, 0, getResources().getString(R.string.MenuQuit)).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
        return true;
    }

    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case EventAdapterActivity.MENU_BACK:
        	startActivityForResult(new Intent(this,MainActivity.class), MainActivity.CLOSING);
        	finish();
            return true;
        case EventAdapterActivity.MENU_HELP:
            final CharSequence[] items = {"This calendar includes the following highlights:\nRed: during this day at least one event was programmed and wasn' sent.\nBlue: an event is programmed for this day and still hasn't been reached\nRed border- today Green: all the past events were programmed and sent"};
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Application info: Calendar");
            builder.setItems(items, new DialogInterface.OnClickListener() {				
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});        	
            builder.create();
            return true;
        case EventAdapterActivity.MENU_QUIT:
        	Bundle bundle = new Bundle();
        	bundle.putInt(MainActivity.CLOSING_PARAM, 1);
        	Intent i = new Intent(this,MainActivity.class);
        	i.putExtras(bundle);
        	setResult(RESULT_OK, i);
        	DatabaseManager.close();
        	finish(); 
            return true;
        }
        return false;
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	Bundle extras = data.getExtras();
    	if(extras.getInt(MainActivity.CLOSING_PARAM) == 1)
    		finish();  	
    }
}
